package org.example.wx.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.example.db.entity.Cart;
import org.example.db.mapper.CartMapper;
import org.example.security.service.AuthService;
import org.example.wx.dto.GoodsIdGoodsSpecIdDto;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WxCartService {

    CartMapper cartMapper;
    AuthService authService;

    public WxCartService(CartMapper cartMapper, AuthService authService) {
        this.cartMapper = cartMapper;
        this.authService = authService;
    }

    public void addCart(GoodsIdGoodsSpecIdDto cartMsg){

        Long wxUserId = authService.getWxUserId();
        Cart cart = new Cart();
        cart.setGoodsSpecId(cartMsg.getGoodsSpecId());
        cart.setWxUserId(wxUserId);
        cart.setNumber(cartMsg.getNumber());
        cartMapper.insert(cart);
    }

    public List<Cart> listCart(){

        Long wxUserId = authService.getWxUserId();
        return cartMapper.selectList(Wrappers.lambdaQuery(Cart.class).eq(Cart::getWxUserId, wxUserId));
    }

    public void deleteCart(Long goodsSpecId){
        Long wxUserId = authService.getWxUserId();
        cartMapper.delete(Wrappers.lambdaQuery(Cart.class).eq(Cart::getWxUserId, wxUserId).eq(Cart::getGoodsSpecId, goodsSpecId));

    }

    public int cartCount(){
        return cartMapper.selectCount(Wrappers.lambdaQuery(Cart.class).eq(Cart::getWxUserId, authService.getWxUserId()));
    }
}
